package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.truck;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TTruckBlacklist;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.TTruckBlacklistServiceImpl;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/truckBlackList")
public class TruckBlackListController {
    @Autowired
    private TTruckBlacklistServiceImpl truckBlacklistService;
    @Autowired
    private UserInfoDao userInfoDao;
    @PostMapping("/getAllTruck")
    public SzResult getAllTruck(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        Integer page = jsonObject.getInteger("page");
        Integer pageSize = jsonObject.getInteger("pageSize");
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        String truckNo = jsonObject.getString("truckNo");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBlacklistService.getAllTruck(page, pageSize,truckNo, startTime, endTime, plantId);
    }

    @PostMapping ("/addTruck")
    public SzResult addGypsumPlan(@RequestBody TTruckBlacklist truckBlacklist, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String plantId = request.getHeader("Plant-Id");
        return truckBlacklistService.addTruck(truckBlacklist,user);
    }
    @PostMapping ("/updateTruck")
    public SzResult updateGypsumPlan(@RequestBody TTruckBlacklist truckBlacklist, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        String plantId = request.getHeader("Plant-Id");
        return truckBlacklistService.updateTruck(truckBlacklist,user);
    }


    @PostMapping ("/delTruck")
    public SzResult delTruck(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String id = jsonObject.getString("id");
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return truckBlacklistService.delTruck(id);
    }
}
